library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.2.1 ✓ purrr 0.3.3
## ✓ tibble 2.1.3 ✓ dplyr 0.8.4
## ✓ tidyr 1.0.2 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.4.0
## ── Conflicts ────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(sf)
## Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
library(ggiraph)
library(maps)
##
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
##
## map
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(patchwork) library(RColorBrewer)
vets <- read_csv("rural_veterans/data/rural_vets_complete_clean.csv")
## Parsed with column specification:
## cols(
## .default = col_double(),
## state = col_character(),
## rural_urban = col_character(),
## format = col_character(),
## region = col_character()
## )
## See spec(...) for full column specifications.
borough_info <- c(“BRONX; Population: 1,332,650; Number of SQF incidents: 135,724”, “BROOKLYN; Population: 2,465,326; Number of SQF incidents: 228,354”, “MANHATTAN; Population: 1,537,195; Number of SQF incidents: 140,913”, “QUEENS; Population: 2,229,379; Number of SQF incidents: 152,681”, “STATEN ISLAND; Population: 443,728; Number of SQF incidents: 28,052”) freq_sqf <- cbind(freq_sqf, borough_info) datamap <- inner_join(freq_sqf, nyc_map)
To make interactive:
library(plotly) data <- data %>% arrange(pop) %>% mutate( name=factor(name, unique(name))) %>% mutate( mytext=paste( “City:”, name, “”, “Population:”, pop, sep="") )
p <- data %>% ggplot() + geom_polygon(data = UK, aes(x=long, y = lat, group = group), fill=“grey”, alpha=0.3) + geom_point(aes(x=long, y=lat, size=pop, color=pop, text=mytext, alpha=pop) ) + scale_size_continuous(range=c(1,15)) + scale_color_viridis(option=“inferno”, trans=“log” ) + scale_alpha_continuous(trans=“log”) + theme_void() + ylim(50,59) + coord_map() + theme(legend.position = “none”)
p <- ggplotly(p, tooltip=“text”) p
# obtain map data of PA and some neighboring states
us_map <- map_data("state")
# create visualization
cols <- c("Rural" = "#F4200B", "Urban" = "#2C0A90", "Total" = "#F4D03F",
"West" = "#E67E22", "Midwest" = "#F1C40F", "Northeast" = "#C0392B",
"South" = "#2980B9")
# Reorder data + Add a new column with tooltip text
vets_map <- vets_map %>%
mutate(rural_urban = fct_relevel(rural_urban, c("Total", "Urban", "Rural"))) %>%
mutate(state = factor(state, unique(state))) %>%
mutate(caption = paste(
state, "\n",
"Population: ", total, sep="")
)
p <- ggplot() +
geom_polygon(data = us_map, aes(x=long, y=lat, group=group),
color="#85C1E9", fill = "#EBF5FB") +
geom_point(data = vets_map, aes(x = x, y = y, size = total, color = rural_urban, alpha = .7,
text = caption)) +
scale_size_continuous(range=c(1,15)) +
scale_color_manual(values = cols) +
coord_map()
## Warning: Ignoring unknown aesthetics: text
p <- ggplotly(p, tootltip = "text")
p
vets %>%
filter(rural_urban == "Rural") %>%
ggplot(aes(x = reorder(state, -in_poverty), y = in_poverty, fill = region)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = cols, name = "Region") +
theme_light() +
theme(plot.title = element_text(color = "#1A5276", size = 18, face = "bold"),
axis.title.x = element_text(color = "#1A5276", size = 11, face = "bold"),
axis.title.y = element_text(color = "#1A5276", size = 11, face = "bold"),
axis.text.x = element_text(color = "gray45", size = 8, angle = 70, hjust = 1),
axis.ticks = element_blank(),
legend.title = element_text(color = "#1A5276", size = 11, face = "bold"))+
labs(title = "Rural Veterans Living in Poverty", y = "Percent in poverty", x = "State")